package org.zsk.cs.util;

/**
 * 验证码类
 * @author zsk
 * @version 1.0
 * Create on 2019/11/17
 */
public class VerifyCode {
	/**
	 * 验证码字符串
	 */
	private String verifyCode;
	/**
	 * 验证码元素字符串
	 */
	private String sourceStr;
	/**
	 * 验证码元素数组
	 */
	private String[] arrStr;
	/**
	 * 该类单例
	 */
	private static VerifyCode instance;
	private VerifyCode() {
		this.sourceStr="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
			    +",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
		this.arrStr=sourceStr.split(",");
	}
	public static VerifyCode getInstance() {
		if(instance==null) {
			instance=new VerifyCode();
		}
		return instance;
	}
	/**
	 * 生成4位验证码
	 * @return 4位验证码
	 */
	public String createVerifyCode() {
		verifyCode="";
	    int index=0;
	    for(int i=0;i<4;i++){
	    index=(int)(Math.random()*arrStr.length);
	    verifyCode+=arrStr[index];
	    }
	    return verifyCode;
	}
	/**
	 * 生成自定义长度验证码
	 * @param verifyCodeLength 自定义验证码长度
	 * @return 验证码
	 */
	public String createVerifyCode(int verifyCodeLength) {
		verifyCode="";
	    int index=0;
	    for(int i=0;i<verifyCodeLength;i++){
	    index=(int)(Math.random()*arrStr.length);
	    verifyCode+=arrStr[index];
	    }
	    return verifyCode;
	}

}
